#ifndef CIRCULECROSSCALCULATE_H
#define CIRCULECROSSCALCULATE_H
//https://www.xuebuyuan.com/3221780.html
#include<math.h> // sqrt fabs
#include<stdio.h>
#include <QDebug>

// 点
struct point_t {
    double x, y;
};

// 圆
struct circle_t {
    struct point_t center;
    double r;
};

// 浮点数判同
int double_equals(double const a, double const b)
{
    static const double ZERO = 1e-9;
    return fabs(a - b) < ZERO;
}

// 两点之间距离的平方
double getDistance_sqr(struct point_t const* a, struct point_t const* b)
{
    return (a->x - b->x) * (a->x - b->x) + (a->y - b->y) * (a->y - b->y);
}

// 两点之间的距离
double getDistanceOfPoint(struct point_t const* a, struct point_t const* b)
{
//    double tmpReturn=getDistance_sqr(a, b);
//    qDebug()<<"tmpReturn1"<<tmpReturn;
//    tmpReturn=sqrt(tmpReturn);
//    qDebug()<<"tmpReturn2"<<tmpReturn;
//    return tmpReturn;
    return sqrt(getDistance_sqr(a, b));
}

/*
* 两圆相交函数
* 参数:
*    circles[0] 和 circles[1] 分别是两个圆.
*    points[0] 和 points[1] 用来存放交点数值, 虽然有些情况下两个不都会用上;
*        如果用到了两个交点, 那么返回后, 横坐标大的在前, 如果横坐标一样, 则纵坐标大的在前.
* 返回值:
*    -1 如果两个圆一模一样;
*    其它整数值: 交点个数.
*/
int insect(struct circle_t circles[], struct point_t points[])
{
    double d, a, b, c, p, q, r; // a, b, c, p, q, r 与上面分析中的量一致
    double cos_value[2], sin_value[2]; // 交点的在 circles[0] 上对应的正余弦取值
                                       // 余弦值 cos_value 就是分析中的 cosθ
    if (double_equals(circles[0].center.x, circles[1].center.x)
        && double_equals(circles[0].center.y, circles[1].center.y)
        && double_equals(circles[0].r, circles[1].r)) {
        return -1;
    }
//    qDebug()<<"111circle1"<<circles[0].center.x<<circles[0].center.y
//           <<"circle2"<<circles[1].center.x<<circles[1].center.y;
    d = getDistanceOfPoint(&circles[0].center, &circles[1].center); // 圆心距离
    if (d > circles[0].r + circles[1].r
        || d < fabs(circles[0].r - circles[1].r)
            || d<0.000001)
    {
//        qDebug()<<"insect d"<<d<<circles[0].r<<circles[1].r;
        return 0;
    }

    a = 2.0 * circles[0].r * (circles[0].center.x - circles[1].center.x);
    b = 2.0 * circles[0].r * (circles[0].center.y - circles[1].center.y);
    c = circles[1].r * circles[1].r - circles[0].r * circles[0].r
        - getDistance_sqr(&circles[0].center, &circles[1].center);
    p = a * a + b * b;
    q = -2.0 * a * c;

    // 如果交点仅一个
    if (double_equals(d, circles[0].r + circles[1].r)
        || double_equals(d, fabs(circles[0].r - circles[1].r))) {
        cos_value[0] = -q / p / 2.0;
        sin_value[0] = sqrt(1 - cos_value[0] * cos_value[0]);

        points[0].x = circles[0].r * cos_value[0] + circles[0].center.x;
        points[0].y = circles[0].r * sin_value[0] + circles[0].center.y;

        // 在这里验证解是否正确, 如果不正确, 则将纵坐标符号进行变换
        if(!double_equals(getDistance_sqr(&points[0], &circles[1].center),
                          circles[1].r * circles[1].r)) {
            points[0].y = circles[0].center.y - circles[0].r * sin_value[0];
        }
        return 1;
    }

    r = c * c - b * b;
    cos_value[0] = (sqrt(q * q - 4.0 * p * r) - q) / p / 2.0;
    cos_value[1] = (-sqrt(q * q - 4.0 * p * r) - q) / p / 2.0;
    sin_value[0] = sqrt(1 - cos_value[0] * cos_value[0]);
    sin_value[1] = sqrt(1 - cos_value[1] * cos_value[1]);

    points[0].x = circles[0].r * cos_value[0] + circles[0].center.x;
    points[1].x = circles[0].r * cos_value[1] + circles[0].center.x;
    points[0].y = circles[0].r * sin_value[0] + circles[0].center.y;
    points[1].y = circles[0].r * sin_value[1] + circles[0].center.y;

    // 验证解是否正确, 两个解都需要验证.
    if (!double_equals(getDistance_sqr(&points[0], &circles[1].center),
                       circles[1].r * circles[1].r)) {
        points[0].y = circles[0].center.y - circles[0].r * sin_value[0];
    }
    if (!double_equals(getDistance_sqr(&points[1], &circles[1].center),
                       circles[1].r * circles[1].r)) {
        points[1].y = circles[0].center.y - circles[0].r * sin_value[1];
    }
    // 如果求得的两个点坐标相同, 则必然其中一个点的纵坐标反号可以求得另一点坐标
    if (double_equals(points[0].y, points[1].y)
        && double_equals(points[0].x, points[1].x)) {
        if(points[0].y > 0) {
            points[1].y = -points[1].y;
        } else {
            points[0].y = -points[0].y;
        }
    }
    return 2;
}




int circulate_cross_test()
{
    struct circle_t circles[2];
    struct point_t points[2];
    printf("请输入两圆x，y，半径(以逗号分开)：");
    while (EOF != scanf("%lf,%lf,%lf,%lf,%lf,%lf",
                   &circles[0].center.x, &circles[0].center.y, &circles[0].r,
                   &circles[1].center.x, &circles[1].center.y, &circles[1].r)) {
        switch (insect(circles, points)) {
            case -1:
                printf("THE CIRCLES ARE THE SAME/n");
                break;
            case 0:
                printf("NO INTERSECTION/n");
                break;
            case 1:
                printf("(%.3lf %.3lf)\n", points[0].x, points[0].y);
                break;
            case 2:
                printf("(%.3lf %.3lf) (%.3lf %.3lf)\n",
                       points[0].x, points[0].y,
                       points[1].x, points[1].y);
        }
    }
    return 0;
}


#endif // CIRCULECROSSCALCULATE_H
